const cloud = require('wx-server-sdk')
cloud.init({
    env: cloud.DYNAMIC_CURRENT_ENV
})

const db = cloud.database()

exports.main = async (event, context) => {
    try {
        const { page = 1, pageSize = 10, searchKeyword = '' } = event

        // 构建查询条件
        const where = {}
        if (searchKeyword) {
            where.title = db.RegExp({
                regexp: searchKeyword,
                options: 'i'
            })
        }

        // 查询数据
        const res = await db.collection('case_resume_template')
            .where(where)
            .orderBy('_id', 'desc')
            .skip((page - 1) * pageSize)
            .limit(pageSize)
            .get()

        // 获取总数用于分页
        const countRes = await db.collection('case_resume_template')
            .where(where)
            .count()

        return {
            code: 0,
            data: {
                list: res.data,
                total: countRes.total,
                hasMore: page * pageSize < countRes.total
            }
        }
    } catch (err) {
        console.error('获取简历案例失败:', err)
        return {
            code: -1,
            message: err.message
        }
    }
}